package com.ogawa.joypal.user.viewmodel

import androidx.lifecycle.Lifecycle
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.OnLifecycleEvent
import androidx.lifecycle.viewModelScope
import com.blankj.utilcode.util.LogUtils
import com.ogawa.base.base.BaseViewModel
import com.ogawa.base.bean.LoadState
import com.ogawa.projectcommon.utils.dataConvert
import com.ogawa.projectcommon.utils.launch
import com.ogawa.projectcommon.bean.UpdateRequestBean
import com.ogawa.projectcommon.bean.UpdateResponseBean
import com.ogawa.projectcommon.network.RetrofitManager
import com.ogawa.joypal.user.bean.UserInfosBean
import com.ogawa.joypal.user.service.UserApiService
import kotlinx.coroutines.cancel

class UserInfoViewModel : BaseViewModel() {
    var mUserInfosBean = MutableLiveData<UserInfosBean>()
    var mUpdateResponseBean = MutableLiveData<UpdateResponseBean>()


    fun  checkUpdate(request: UpdateRequestBean){
        launch({
//            loadState.value = LoadState.Loading("")
//            mUpdateResponseBean.value = RetrofitManager.INSTANCE.createService(UserApiService::class.java)
//                .checkUpdate(request)
//                .dataConvert()
//            loadState.value = LoadState.Success("")
        }
            , { loadState.value = LoadState.Fail(it.message ?: "") }
            , {})
    }

    fun  userInfo(id:String){
        launch({
            loadState.value = LoadState.Loading("")

            mUserInfosBean.value = RetrofitManager.INSTANCE.createService(UserApiService::class.java)
                .userInfo(id)
                .dataConvert()
            loadState.value = LoadState.Success("")
        }
            , { loadState.value = LoadState.Fail(it.message ?: "") }
            , {})
    }




     @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    open fun onDestroy() {
        LogUtils.e("onDestroy")
        viewModelScope.cancel()
    }


}
